package com.study_day01.config;

import com.study_day01.entiry.ResponseData;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseEntity<ResponseData> handleBusinessException(BusinessException ex) {
        ResponseData responseData = new ResponseData();
        responseData.setCode("BUSINESS_ERROR");
        responseData.setErrorMsq(ex.getMessage());
        return ResponseEntity.badRequest().body(responseData);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseEntity<ResponseData> handleValidationExceptions(MethodArgumentNotValidException ex) {
        ResponseData responseData = new ResponseData();
        responseData.setCode("VALIDATION_ERROR");
        responseData.setErrorMsq(ex.getMessage());
        return ResponseEntity.badRequest().body(responseData);
    }
}
